from PySide import QtCore
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
from PySide import QtGui

from ui_elements.window import Ui_todo_window
from todo_table import TodoTable
from tray_icon import TrayIcon
from main_window import MainWindow
from settings import Settings
from task_dialog import TaskDialog


class UI(Ui_todo_window):
    def __init__(self):
        super(UI, self).__init__()
        self.window = MainWindow()
        self.icon = QIcon('scheduled_tasks.ico')
        self.setupUi(self.window)
        
    def setupUi(self, window):
        super(UI, self).setupUi(window)
        self.window.setWindowIcon(self.icon)
        self.setup_menu()
        self.setup_table()
        self.setup_tray_icon()
        self.setup_task_dialog()
        self.setup_settings()
        self.setup_actions()
        self.add_actions()
        
    def setup_menu(self):
        self.menu = QMenu()
        self.menu.addMenu('File')
        self.menu_button.clicked.connect(self.show_menu)
        
    def setup_actions(self):
        self.quit_act = QAction("&Quit", self.window, shortcut=Qt.CTRL+Qt.Key_Q, triggered=QtGui.qApp.quit)
        self.quit_act.setMenuRole(QAction.QuitRole)
        self.quit_act.setShortcutContext(Qt.ApplicationShortcut)
        self.close_window_act = QAction("&Close Window", self.window, shortcut=Qt.CTRL+Qt.Key_W, triggered=self.window.close)
        self.add_act = QAction("&Add", self.window, shortcut=Qt.Key_Plus, triggered=self.add_activated)
        self.remove_act = QAction("&Remove", self.window, shortcut=Qt.Key_Minus, triggered=self.remove_activated)
        self.settings_act = QAction("&Settings", self.window, triggered=self.settings_activated)
        
    def add_actions(self):
        self.menu.addAction(self.add_act)
        self.menu.addAction(self.remove_act)
        self.menu.addSeparator()
        self.menu.addAction(self.settings_act)
        self.menu.addSeparator()
        self.menu.addAction(self.close_window_act)
        self.menu.addSeparator()
        self.menu.addAction(self.quit_act)
        
        self.window.addAction(self.quit_act)
        self.window.addAction(self.close_window_act)
        self.window.addAction(self.add_act)
        self.window.addAction(self.remove_act)
        
        self.tray_icon.contextMenu().addAction(self.settings_act)
        self.menu.addSeparator()
        self.tray_icon.contextMenu().addAction(self.quit_act)
        
    def setup_table(self):
        self.table = TodoTable(self.todo_table)
        self.table.edit_item.connect(self.edit_item)
        self.table.setup()
        
    def setup_tray_icon(self):
        self.tray_icon = TrayIcon(self.icon)
        self.tray_icon.show()
        self.tray_icon.activated.connect(self.tray_icon_activated)
        
    def setup_task_dialog(self):
        self.task_dialog = TaskDialog(self.icon)
        
    def setup_settings(self):
        self.settings = Settings(self.icon)
        
    def show(self):
        self.window.show()
        
    def show_menu(self):
        pos = self.menu_button.mapToGlobal(QPoint(self.menu_button.frameSize().width()/2, self.menu_button.frameSize().height()/2))
        self.menu.popup(pos)
        
    def tray_icon_activated(self, reason):
        print "tray icon clicked: %s" % reason
        if reason == QSystemTrayIcon.DoubleClick:
            self.window.show()
        elif reason == QSystemTrayIcon.Trigger:
            pass
        
    @QtCore.Slot(int)
    def edit_item(self, id):
        print "edit item", id
        self.task_dialog.clear()
        query = QSqlQuery()
        query.prepare("select task, category from tasks where id=?")
        query.bindValue(0, id)
        query.exec_()
        query.first()
        task = query.value(0)
        category = query.value(1)
        self.task_dialog.task_entry.setText(task)
        self.task_dialog.category_combobox.setCurrentIndex(category)
        self.task_dialog.show(TaskDialog.REASON_EDIT, id)
        
    def add_activated(self):
        print 'add'
        self.task_dialog.clear()
        self.task_dialog.show(TaskDialog.REASON_ADD)
        
    def remove_activated(self):
        print 'remove'
        
    def settings_activated(self):
        print 'settings'
        self.settings.show()
